﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class getPlayersByTeam : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        public int teamID { get; set; }

        public getPlayersByTeam(HttpContext context)
            : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            StringBuilder sb = new StringBuilder();
            
            try 
            { 
                Team t = (from x in dbContext.Teams where x.TeamID == teamID select x).FirstOrDefault();

                foreach (PlayersTeam z in t.PlayersTeams.AsEnumerable())
                {
                   
                     sb.Append(z.Player.IPadID +";");
                     sb.Append(z.Player.Name );
                    sb.Append("\n\r");
                  
                }
                return sb.ToString();
            }
            catch (Exception)
            {
                return "ERROR";
            }
        }

        public override void InitializeProperties()
        {
            try
            {
                teamID = Convert.ToInt32(Parameters["teamid"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}